/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.order.entity.ApplyCampaign
   Module Description   :

   Date Created      : 2010/7/6
   Original Author   : jeffma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.order.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;

import com.yaodian100.ec.campaign.entity.Campaign;

/**
 * @author jeffma
 * 
 */
@Entity
@Table(name = "EC_ORDER_CAMPAIGN")
public class ApplyCampaign implements Serializable {

	/** serialVersionUID */
	private static final long serialVersionUID = -7576534992471788907L;
	/** uuid */
	@Id
	@GeneratedValue(generator = "gen_uuid")
	@GenericGenerator(name = "gen_uuid", strategy = "uuid")
	@Column(name = "OBJ_UUID")
	private String uuid;
	/** isActive */
	@Column(name = "IS_ACTIVE", columnDefinition = "NUMBER(1)")
	private boolean isActive = false;
	/** campaign object */
	@Column(name = "CAMPAIGN_UUID", length = 40)
	private String campaignUuid;
	/**
	 * 活动名称 Campaign Name
	 */
	@Column(name = "CAMPAIGN_NAME", length = 100, nullable = false)
	private String campaignName;
	/** campaignUrl, 放置活動公告靜態網頁 */
	@Column(name = "CAMPAIGN_URL", length = 200)
	private String campaignUrl;
	/** applyProductIds */
	@Transient
	private List<String> applyProductIds;
	@Column(name = "APPLY_PRODUCTS", length = 300, updatable = false)
	private String applyProductIdString;
	/** sumPrice */
	@Column(name = "SUM_AMOUNT", columnDefinition = "numeric(10,2)")
	private BigDecimal sumAmount = BigDecimal.ZERO;
	/** sumQty */
	@Column(name = "SUM_QTY", columnDefinition = "numeric(5)")
	private int sumQty = 0;
	@Column(name = "DISCOUNT", columnDefinition = "numeric(10,2)")
	private BigDecimal discount = BigDecimal.ZERO;
	/** modifyDate */
	@Version
	@Column(name = "MODIFY_DATE")
	@Temporal(TemporalType.TIMESTAMP)
	private Date modifyDate;
	/** order */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ORDER_ID", nullable = false)
	private Orders order;

	/** budget, 预算归属, 維護於選單 Menu 'Budget' */
	@Column(name = "BUDGET", length = 2)
	private String budget;

	/** default constructor */
	public ApplyCampaign() {
		super();
	}

	public ApplyCampaign(Orders order, com.yaodian100.ec.shopping.entity.ApplyCampaign ac) {
		super();
		this.order = order;
		this.sumAmount = ac.getSumAmount();
		this.sumQty = ac.getSumQty();
		this.campaignUuid = ac.getCampaign().getUuid();
		this.campaignName = ac.getCampaign().getCampaignName();
		this.campaignUrl = ac.getCampaign().getCampaignUrl();
		this.applyProductIds = ac.getApplyProductIds();
		this.applyProductIdString = StringUtils.join(applyProductIds, ",");
		this.isActive = ac.isActive();
		if (this.isActive) {
			this.discount = ac.getSumDiscount();
		}
		this.budget = ac.getCampaign().getBudget();
	}

	public ApplyCampaign(Orders order, Campaign c) {
		super();
		this.order = order;
		// this.sumAmount = ac.getSumAmount();
		// this.sumQty = ac.getSumQty();
		// this.applyProductIds = ac.getApplyProductIds();
		// this.applyProductIdString = StringUtils.join(applyProductIds, ",");
		// this.isActive = ac.isActive();
		// if (this.isActive) {
		// this.discount = ac.getSumDiscount();
		// }
		this.campaignUuid = c.getUuid();
		this.campaignName = c.getCampaignName();
		this.campaignUrl = c.getCampaignUrl();
		this.budget = c.getBudget();
	}

	/**
	 * @return the budget
	 */
	public String getBudget() {
		return budget;
	}

	/**
	 * @param budget the budget to set
	 */
	public void setBudget(String budget) {
		this.budget = budget;
	}

	/**
	 * @return the isActive
	 */
	public boolean isActive() {
		return isActive;
	}

	/**
	 * @param isActive the isActive to set
	 */
	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	/**
	 * @return the campaignUrl
	 */
	public String getCampaignUrl() {
		return campaignUrl;
	}

	/**
	 * @param campaignUrl the campaignUrl to set
	 */
	public void setCampaignUrl(String campaignUrl) {
		this.campaignUrl = campaignUrl;
	}

	/**
	 * @return the campaignName
	 */
	public String getCampaignName() {
		return campaignName;
	}

	/**
	 * @param campaignName the campaignName to set
	 */
	public void setCampaignName(String campaignName) {
		this.campaignName = campaignName;
	}

	/**
	 * @return the applyProductIdString
	 */
	public String getApplyProductIdString() {
		applyProductIdString = StringUtils.join(applyProductIds, ",");
		return applyProductIdString;
	}

	/**
	 * @param applyProductIdString the applyProductIdString to set
	 */
	public void setApplyProductIdString(String applyProductIdString) {
		applyProductIds = new ArrayList();
		if (StringUtils.isNotBlank(applyProductIdString)) {
			this.applyProductIdString = applyProductIdString;
			for (String s : StringUtils.split(applyProductIdString, ",")) {
				applyProductIds.add(s);
			}
		}
	}

	/**
	 * @return the uuid
	 */
	public String getUuid() {
		return uuid;
	}

	/**
	 * @param uuid the uuid to set
	 */
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	/**
	 * @return the campaignUuid
	 */
	public String getCampaignUuid() {
		return campaignUuid;
	}

	/**
	 * @param campaignUuid the campaignUuid to set
	 */
	public void setCampaignUuid(String campaignUuid) {
		this.campaignUuid = campaignUuid;
	}

	/**
	 * @return the applyProductIds
	 */
	public List<String> getApplyProductIds() {
		if ((applyProductIds == null) && StringUtils.isNotBlank(applyProductIdString)) {
			applyProductIds = new ArrayList();
			for (String s : StringUtils.split(applyProductIdString, ",")) {
				applyProductIds.add(s);
			}
		}
		return applyProductIds;
	}

	/**
	 * @param applyProductIds the applyProductIds to set
	 */
	public void setApplyProductIds(List<String> applyProductIds) {
		this.applyProductIds = applyProductIds;
	}

	/**
	 * @return the sumAmount
	 */
	public BigDecimal getSumAmount() {
		return sumAmount;
	}

	/**
	 * @param sumAmount the sumAmount to set
	 */
	public void setSumAmount(BigDecimal sumAmount) {
		this.sumAmount = sumAmount;
	}

	/**
	 * @return the order
	 */
	public Orders getOrder() {
		return order;
	}

	/**
	 * @param order the order to set
	 */
	public void setOrder(Orders order) {
		this.order = order;
	}

	/**
	 * @return the sumQty
	 */
	public int getSumQty() {
		return sumQty;
	}

	/**
	 * @param sumQty the sumQty to set
	 */
	public void setSumQty(int sumQty) {
		this.sumQty = sumQty;
	}

	/**
	 * @return the modifyDate
	 */
	public Date getModifyDate() {
		return modifyDate;
	}

	/**
	 * @param modifyDate the modifyDate to set
	 */
	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	/**
	 * @return the discount
	 */
	public BigDecimal getDiscount() {
		return discount;
	}

	/**
	 * @param discount the discount to set
	 */
	public void setDiscount(BigDecimal discount) {
		this.discount = discount;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "ApplyCampaign [campaignUuid=" + campaignUuid + ", applyProductIdString=" + applyProductIdString
				+ ", applyProductIds=" + applyProductIds + ", sumAmount=" + sumAmount + ", sumQty=" + sumQty + "]";
	}

}
